﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FangChanCrm.Services.Interface;
using FangChanCrmModel;
using Maticsoft.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FangChanCrm.Controllers
{
    public class PhoneFangYuanController : Controller
    {
        private readonly It_FangYuanBiaoService t_FangYuanBiaoService;
        private readonly It_FangYuanXinXiBiaoService t_FangYuanXinXiBiaoService;
        private readonly It_RenYuanBiaoService t_RenYuanBiaoService;
        private readonly It_ZhanQuXiaoQuGuanLianBiaoService t_ZhanQuXiaoQuGuanLianBiaoService;

        public PhoneFangYuanController(It_FangYuanBiaoService FangYuanBiaoService, It_FangYuanXinXiBiaoService FangYuanXinXiBiaoService, It_RenYuanBiaoService RenYuanBiaoService, It_ZhanQuXiaoQuGuanLianBiaoService ZhanQuXiaoQuGuanLianBiaoService)
        {
            t_FangYuanBiaoService = FangYuanBiaoService;
            t_FangYuanXinXiBiaoService = FangYuanXinXiBiaoService;
            t_RenYuanBiaoService = RenYuanBiaoService;
            t_ZhanQuXiaoQuGuanLianBiaoService = ZhanQuXiaoQuGuanLianBiaoService;
        }
        public IActionResult FangYuanList()
        {
            return View();
        }
        public IActionResult FangYuanXiangXi()
        {
            return View();
        }
        public IActionResult FangYuanXinXiList()
        {
            return View();
        }
        public IActionResult FangDaiJiSuanQi()
        {
            return View();
        }
        public string Getfangyuan()
        {
            //int id = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
            //t_RenYuanBiao ry = t_RenYuanBiaoService.GetById(id);
            //List<t_ZhanQuXiaoQuGuanLianBiao> zq = t_ZhanQuXiaoQuGuanLianBiaoService.GetList(ry);

            //string returnstr = "Id != 1";
            //for (int i = 0; i < zq.Count; i++)
            //{
            //    if (i == 0) {
            //        returnstr += " and Id ==" + zq[i].XiaoQuId;
            //    }
            //    else 
            //    {
            //        returnstr += " or Id ==" + zq[i].XiaoQuId;
            //    }
            //}

            List<t_FangYuanBiao> fy = t_FangYuanBiaoService.GetList1();
            StringBuilder Json = new StringBuilder();
            int xunhuan = 10;
            int tianchong = 0;
            if (fy.Count >= 10)
            {
                xunhuan = xunhuan + xunhuan * Convert.ToInt32(Request.Form["cishu"]);
                if (fy.Count < xunhuan)
                {
                    tianchong = xunhuan - fy.Count;
                    xunhuan = xunhuan - tianchong;
                }
            }
            else
            {
                xunhuan = fy.Count;
            }

            for (int i = 0; i < xunhuan; i++)
            {
                //Json.Append("<div style='border-style:solid;border-width:1px 1px 1px 1px; border-color:black;margin:2px 1px 1px 1px; background-color:white;'onclick='tiaozhuan(" + fy[i].Id+")'><h1 style='margin: 1px 1px 1px 1px;'> "+ fy [i].XiaoQuMingCheng+ " </h1></div>")
                //Json.Append("<div onclick='tiaozhuan(" + fy[i].Id + ")' style='border-left:groove;margin-left:45px;padding-top:10px' ><span class='icon-circle icon-50bg-light-primary mr-3' style='margin: 0px 0px 0px 10px; height: 8%; width: 15%; line-height: 47px; vertical-align: middle; text-align: center; display: inline-block; border-radius: 50 %; margin-right: 1rem!important; background-color: rgba(121, 121, 255, 0.15)!important; color: #7979ff; '><i class='material-icons' style='vertical-align: middle; font-size: 1.5em; font-family: 'Material Icons'; font-weight: normal; font-style: normal; display: inline-block; line-height: 1; text-transform: none; letter-spacing: normal; word-wrap: normal; white-space: nowrap; direction: ltr; -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility; -moz-osx-font-smoothing: grayscale; font-feature-settings: 'liga'; '>business</i></span><span >" + fy[i].XiaoQuMingCheng + "</span><hr style = 'margin-left: 10px;margin-top:10px;margin-bottom:0px;width:80%; text-align: center;height:2px;border:none;border-top:1px solid #555555;' /></div>");
                //Json.Append("<div class='row' onclick='tiaozhuan(" + fy[i].Id + ")' style='margin: 0px 0px 0px 0px; border-left: 5px solid #0094ff; padding-bottom: 10px '><div class=;col-md-10' style='width:85%'><span class='icon-circle icon-50 bg-light-primary mr-3' style='margin: 0px 0px 0px 10px; height: 45px; width: 45px; background-color: rgba(121, 121, 255, 0.15); color: #7979ff;'><i class='material-icons' style='padding-bottom:5px;vertical-align: middle; font-size: 23px; font-family: 'Material Icons'; font-weight: normal; font-style: normal; display: inline-block; line-height: 1; text-transform: none; letter-spacing: normal; word-wrap: normal; white-space: nowrap; direction: ltr; -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility; -moz-osx-font-smoothing: grayscale; font-feature-settings: 'liga';'>business</i></span><span style = 'font-size:17px;margin-top: 10px !important;'>" + fy[i].XiaoQuMingCheng + "</span></div><div class='col-md-2' style='width:10%;height:15px;padding-top:10px'><img style='text-align:right;width:15px;height:15px' src='/wwwroot/GoTRI/img/dayuhao.jpg'/></div><hr style = 'margin-left: 10px;margin-top:10px;margin-bottom:0px;width:90%; text-align: center;height:2px;border:none;border-top:2px solid #C0C0C0;' /></div>");
                if (fy.Count == i + 1)
                {
                    Json.Append("<li onclick='tiaozhuan(" + fy[i].Id + ")'><div><img src='/GoTRI/img/icon.png' /><span>" + fy[i].XiaoQuMingCheng + "</span></div><div><img src='/GoTRI/img/right.png' /></div></li><script>$('#gengduo').attr('hidden', '');$('#meiyou').removeAttr('hidden', '');</script>");
                }
                else
                {
                    Json.Append("<li onclick='tiaozhuan(" + fy[i].Id + ")'><div><img src='/GoTRI/img/icon.png' /><span>" + fy[i].XiaoQuMingCheng + "</span></div><div><img src='/GoTRI/img/right.png' /></div></li>");
                }
            }
            return Json.ToString();
        }

        public string GetxiaoquMingCheng()
        {
            int id = Convert.ToInt32(Request.Form["id"]);
            t_FangYuanBiao ty = t_FangYuanBiaoService.GetIdAsync(id);
            return ty.XiaoQuMingCheng;
        }

        public string Getfangyuanxiangxi()
        {
            int id = Convert.ToInt32(Request.Form["id"]);
            List<t_FangYuanXinXiBiao> fy = t_FangYuanXinXiBiaoService.GetLouHao(id);
            StringBuilder Json = new StringBuilder();
            for (int i = 0; i < fy.Count; i++)
            {
                List<t_FangYuanXinXiBiao> fhangwu = t_FangYuanXinXiBiaoService.GetFangWuShuJu(Convert.ToInt32(id), fy[i].LouHao);
                int zoushu = fhangwu.Count;
                int chushou = 0;
                int weichushou = 0;
                int shenhezhong = 0;

                for (int j = 0; j < fhangwu.Count; j++)
                {
                    t_FangYuanXinXiBiao fxy = fhangwu[j];
                    if (fxy.ZhuangTai == "已出售")
                    {
                        chushou = chushou + 1;
                    }
                    else if (fxy.ZhuangTai == "未出售")
                    {
                        weichushou = weichushou + 1;
                    }
                    else if (fxy.ZhuangTai == "审核中")
                    {
                        shenhezhong = shenhezhong + 1;
                    }
                    else
                    {

                    }
                }
                //Json.Append("<div style='margin: 2px 0px 0px 0px; border-style: solid; border-width: 1px 1px 1px 1px; border-color: red; background-color:white; height: 54px; text-align: center' onclick='tiaozhuan("+fy[i].LouHao+")' ><div style= 'margin: 1px 1px 1px 1px;'><div style='width:30%;height:50px;background-color:red;float:left'><span style='line-height: 45px'>"+ fy[i].LouHao+ "号楼</span></div><div style='width:70%;height:50px;float:left'><span style='line-height: 45px'>房 源:"+ zoushu + " </span>&nbsp;&nbsp;&nbsp;<span style='line-height: 45px'>已 售:"+chushou+"</span>&nbsp;&nbsp;&nbsp;<span style='line-height: 45px'>未 售:"+weichushou+"</span></div></div></div>");
                Json.Append("<li class='list-item' onclick=\"tiaozhuanyemian('"+fy[i].LouHao+"')\"><div class='item-top' ><div></div><span> " + fy[i].LouHao + "号楼</span></div><div class='item-bottom' style='display: flex;justify-content: space-between;'><div class='classroom' ><span>房源：</span><span> " + zoushu + "</span></div><div class='sold' ><span>已售：</span><span> " + chushou + "</span></div><div class='unsold' ><span>未售：</span><span> " + weichushou + "</span></div></div></li>");
            }
            return Json.ToString();
        }

        public string ChaKanFangWuShu()
        {
            StringBuilder Json = new StringBuilder();

            string xiaoqu = Request.Form["xiaoqu"];
            string louhao = Request.Form["louhao"];

            t_FangYuanBiao fw = t_FangYuanBiaoService.GetIdAsync(Convert.ToInt32(xiaoqu));
            List<t_FangYuanXinXiBiao> fhangwu = t_FangYuanXinXiBiaoService.GetFangWuShuJu(Convert.ToInt32(xiaoqu), louhao);

            string xiaoqumingcheng = fw.XiaoQuMingCheng + "  " + louhao + "号楼";
            int zoushu = fhangwu.Count;
            int chushou = 0;
            int weichushou = 0;
            int shenhezhong = 0;

            for (int i = 0; i < fhangwu.Count; i++)
            {
                t_FangYuanXinXiBiao fy = fhangwu[i];
                if (fy.ZhuangTai == "已出售")
                {
                    chushou = chushou + 1;
                }
                else if (fy.ZhuangTai == "未出售")
                {
                    weichushou = weichushou + 1;
                }
                else if (fy.ZhuangTai == "审核中")
                {
                    shenhezhong = shenhezhong + 1;
                }
                else
                {

                }
            }
            Json.Append("[");
            Json.Append("{\"zoushu\":\"" + zoushu + "\",\"chushou\":\"" + chushou + "\",\"weichushou\":\"" + weichushou + "\",\"shenhezhong\":\"" + shenhezhong + "\",\"xiaoqumingcheng\":\"" + xiaoqumingcheng + "\"}");
            Json.Append("]");
            return Json.ToString();
        }

        public string Getcengshu()
        {
            string xiaoqu = Request.Form["xiaoqu"];
            string louhao = Request.Form["louhao"];
            List<t_FangYuanXinXiBiao> fhangwu = t_FangYuanXinXiBiaoService.GetCeng(Convert.ToInt32(xiaoqu), louhao);
            StringBuilder Json = new StringBuilder();
            for (int i = 0; i < fhangwu.Count; i++)
            {
                Json.Append("<div style='margin: 2px 2px 2px 2px; border-style: solid; border-width: 2px 2px 2px 2px; border-color: black; width:100%; height:40px; line-height: 35px;font-weight:bold'>" + fhangwu[i].Ceng + "F</div>");
            }
            return Json.ToString();
        }

        public string ChaKanXianShi()
        {
            StringBuilder Json = new StringBuilder();
            string yanse = "";
            string xiaoqu = Request.Form["xiaoqu"];
            string louhao = Request.Form["louhao"];

            List<t_FangYuanXinXiBiao> cengshu = t_FangYuanXinXiBiaoService.GetCeng(Convert.ToInt32(xiaoqu), louhao);

            for (int i = 0; i < cengshu.Count; i++)
            {
                int kuandu = 0;
                string htmlMen = "";
                t_FangYuanXinXiBiao fyxinxiceng = cengshu[i];
                List<t_FangYuanXinXiBiao> fyxx = t_FangYuanXinXiBiaoService.GetFangWuBangDing(Convert.ToInt32(xiaoqu), louhao, fyxinxiceng.Ceng);
                for (int j = 0; j < fyxx.Count; j++)
                {
                    t_FangYuanXinXiBiao fy = fyxx[j];
                    if (fy.ZhuangTai == "已出售")
                    {
                        yanse = "red";
                    }
                    else if (fy.ZhuangTai == "未出售")
                    {
                        yanse = "gray";
                    }
                    else if (fy.ZhuangTai == "审核中")
                    {
                        yanse = "green";
                    }
                    else
                    {
                        yanse = "orange";
                    }
                    string shi = "";
                    if (fy.BeiZhu1 != "" && fy.BeiZhu1 != null)
                    {
                        shi = fy.BeiZhu1;
                    }
                    else
                    {
                        string menshuzhi = "";
                        if (Convert.ToInt32(fy.Men) > 9)
                        {
                            menshuzhi = Convert.ToInt32(fy.Men).ToString();
                        }
                        else
                        {
                            menshuzhi = "0" + Convert.ToInt32(fy.Men).ToString();
                        }
                        shi = fy.Ceng + "" + menshuzhi + "室";
                    }
                    kuandu = fyxx.Count * 204;
                    htmlMen += "<li style='display:inline;margin: 2px 2px 0px 2px; border-style: solid; border-width: 2px 2px 2px 2px; border-color: black; width: 200px; height: 40px; background-color: " + yanse + ";float:left;line-height: 35px'>" + shi + "</li>";
                }
                string result = "<ul id='ai' style='width:" + kuandu + "px;background-color:white;list-style-type:none;overflow: hidden;margin-bottom:0px!important'>" + htmlMen + "</ul>";
                Json.Append(result);
            }
            Json.Append("<div style='margin-bottom: 30%; width: 10px'></div>");
            return Json.ToString();
        }

    }
}
